package com.atguigu.dao.impl;

import com.atguigu.dao.DeliveryDao;
import com.atguigu.pojo.Delivery;
import com.atguigu.pojo.DeliveryList;

import java.util.List;

public class DeliveryDaoImpl extends BaseDaoImpl implements DeliveryDao {
    @Override
    public List<DeliveryList> getAllDeliveryById(Integer id) {
        String sql = "SELECT d.id AS id,c.company_name AS companyName,d.delivery_name AS username,d.phone AS phone,d.send_time AS sendTime,d.address AS address,IF (d.state=1,'已签收','未签收') AS 'status' FROM sys_delivery d JOIN sys_company c ON d.company_id=c.id WHERE user_id=?";
        List<DeliveryList> list = getList(DeliveryList.class, sql, id);
        return list;
    }

    @Override
    public void removeDeliveryById(Integer id) {
        String sql = "DELETE FROM sys_delivery WHERE id=?";
        update(sql, id);
    }

    @Override
    public Delivery addDelivery(String username, Integer userId, Integer companyId, String phone, String address, String date, Integer status) {
        String sql = "INSERT INTO sys_delivery(id,user_id,delivery_name,company_id,phone,address,send_time,state) VALUES(null,?,?,?,?,?,?,?)";
        update(sql, userId, username, companyId, phone, address, date, status);
        return getBean(Delivery.class, "SELECT id,delivery_name as deliveryName,user_id as userId,company_id as companyId,phone,address,send_time as sendTime,state FROM sys_delivery WHERE id = (SELECT Max(id) FROM sys_delivery)");
    }

    @Override
    public Delivery getDeliveryById(Integer integer) {
        String sql = "SELECT id,delivery_name as deliveryName,user_id as userId,company_id as companyId,phone,address,send_time as sendTime,state FROM sys_delivery WHERE id=?";
        return getBean(Delivery.class, sql, integer);
    }

    @Override
    public void updateDeliveryById(Integer integer,String deliveryName,Integer companyId,String phone,String address,String date,Integer status) {
        String sql = "UPDATE sys_delivery SET delivery_name=?,company_id=?,phone = ?,address=?,send_time=?,state=? WHERE id=?";
        update(sql, deliveryName, companyId, phone, address, date, status, integer);
    }
}
